<template>
  <div>
  <!-- 商品三级分类 -->
  <common-tree v-if="this.tree.data && this.tree.data.length > 0" :tree="tree"></common-tree>

  <!-- 添加节点弹窗 -->
  <common-dialog-form :from="addAppendChildren"></common-dialog-form>
  </div>

</template>

<script>

  import {successMessage} from "../../../utils/Message";
  import CommonDialogForm from '../../../components/commonDialogForm'
  import CommonFormEnum from '../../../components/enum/CommonFormEnum'
  import CommonTree from '../../../components/commonTree/index.vue'
  export default {
    name: 'productList',
    components:{CommonDialogForm,CommonTree},
    data() {
      return {
        tree:{
          treeProps: {
            label: 'name',
            children: 'children'
          },
           data: [],
           buttons:[
             {butText:'追加节点',click:this.appendChildForm},
               {butText:' 删除节点',click:this.remove},
           ]
        },

        data: [],
        addAppendChildren:{
          show:false,
          from:{
            originalData:{name:null,obj:null},
            title:'添加节点',
            columns:[
              {label:'商品分类名称',prop:'name',type:CommonFormEnum.INPUT,required:true,labelWidth:'120px'}
            ],
            footBut:[
              {api:this.append,text:'添加'}
            ],
          }
        }
      }
    },
    created() {
      this.init()
    },

    methods: {
      /* 初始化商品三级分类 */
      init() {
       return new Promise((resolve, reject) => {
          this.$http({
            url: this.$http.getRequest('product/productcategory/classification'),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            this.tree.data = data.data.data
          })
        })
      },

      /* 追加节点 */
      append(data){
        let obj = {
          name:data.name,
          parentCid:this.addAppendChildren.obj.catId,
          catLevel:this.addAppendChildren.obj.catLevel + 1,
          showStatus:0,
          sort:0,
          productCount:0,
        }
        new Promise((resolve, reject) =>{
          this.$http({
            url: this.$http.getRequest('product/productcategory/save'),
            method: 'post',
            data: this.$http.adornData(obj, false)
          }).then((data) => {
             successMessage("添加节点成功")
            this.init()
            this.addAppendChildren.show=false
          })
        })
      },

      /* 打开追加节点弹窗 */
      appendChildForm(data) {
        this.addAppendChildren.show = true
        this.addAppendChildren.obj=data
      },

       /* 移除节点 */
      remove( data) {
        new Promise((resolve, reject) =>{
          this.$http({
            url: this.$http.getRequest('product/productcategory/delete'),
            method: 'post',
            data: this.$http.adornData(data.catId, false)
          }).then((data) => {
            successMessage("刪除节点成功")
            this.init()
          }).catch(error =>{
             errorMessage("刪除节点失败")
          })
        })
      },
    },
  }
</script>

<style scoped>

</style>
